package com.gxa.modules.service.operationsservice.impl;

import com.gxa.common.R;
import com.gxa.modules.mapper.operationsmapper.ContractMapper;
import com.gxa.modules.pojo.Contract;
import com.gxa.modules.service.operationsservice.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ContractServiceImpl implements ContractService {

    @Autowired(required = false)
    ContractMapper contractMapper;

    @Override
    public R selectForPage(int page, int size, String tenantName, String buildName,
                           String roomName, String property, String contractStatus) {
        R r = new R();
        int index = (page-1)*size;
        List<Contract> list = contractMapper.selectForPage(index, size, tenantName, buildName, roomName, property, contractStatus);
        r.setCode(200);
        r.setMsg("查询成功！");
        r.setCount(contractMapper.selectCount(tenantName, buildName, roomName, property, contractStatus));
        r.setData(list);
        return r;
    }

    @Override
    public R selectById(int id) {
        return null;
    }

    @Override
    public R insert(Contract contract) {
        R r = new R();
        int res = contractMapper.insert(contract);
        if(res>0)
        {
            r.setCode(200);
            r.setMsg("添加成功");
        }
        return r;
    }

    @Override
    public R updateById(Contract contract) {
        R r = new R();
        int res = contractMapper.updateById(contract);
        if(res>0)
        {
            r.setCode(200);
            r.setMsg("修改成功");
        }
        return r;
    }

    @Override
    public R deleteById(int id) {
        R r= new R();
        int res = contractMapper.deleteById(id);
        if(res>0)
        {
            r.setCode(200);
            r.setMsg("删除成功");
        }
        return r;
    }

    @Override
    public R deleteByPatch(int[] ids) {
        R r= new R();
        List<Integer> list = new ArrayList<>();
        for(int i =0;i<ids.length;i++)
        {
            list.add(ids[i]);
        }
        contractMapper.deleteByPatch(list);
        r.setMsg("删除成功");
        r.setCode(200);
        return r;
    }
}
